package com.zhongshengnetwork.rightbe.Adv.model;

import android.content.Context;
import android.content.SharedPreferences;

import java.util.ArrayList;

/**
 * 键值存储管理类
 */
public class SPManageUtils {

    /** 存储的文件名 */
    public static final String DATABASE = "Database";
    /**/


    /**
     * 是否第一次运行
     */
    public static String IS_FIRST_RUN ="isFistRun";





    /**广告保存路径*/
    private static String   ADVERT_SAVE = "advert_save";
    /**广告保存路径数量*/
    private static String   ADVERT_SAVE_NUM = "advert_save_num";

    public static SharedPreferences spf;
    public static SharedPreferences.Editor editor;



    private static SPManageUtils spManage = null;
    private Context mContext;
    private SPManageUtils(Context c){
        this.mContext = c;
        spf = c.getSharedPreferences(DATABASE, Context.MODE_PRIVATE);
        editor = spf.edit();
    }
    public static SPManageUtils getInstance(Context context){
        if(spManage ==null){
            synchronized (SPManageUtils.class){
                if (spManage ==null){
                    spManage = new SPManageUtils(context);
                }
            }
        }
        return  spManage;
    }

    /**
     * @param key
     * @param value
     *  键值存储 put boolean类型
     */
    public void putSPBoolean(String key, boolean value){
        editor.putBoolean(key,value);
        editor.apply();
    }
    /**
     * @param key
     * @param value
     *  键值存储 put String类型
     */
    public void putSPString(String key, String value){
        editor.putString(key,value);
        editor.apply();
    }
    /**
     * @param key
     * @param value
     *  键值存储 put int类型
     */
    public void putSPInt(String key, int value){
        editor.putInt(key,value);
        editor.apply();
    }

    /**
     * @param key
     * @param DefaultValue 默认值
     *  取值 boolean
     */
    public boolean getSPBoolean(String key, boolean DefaultValue){
        return spf.getBoolean(key,DefaultValue);
    }
    /**
     * @param key
     * @param DefaultValue 默认值
     *  取值 String
     */
    public String getSPString(String key, String DefaultValue){
        return spf.getString(key,DefaultValue);
    }

    /**
     * @param key
     * @param DefaultValue 默认值
     *  取值 int
     */
    public int getSPInt(String key, int DefaultValue){
       return spf.getInt(key,DefaultValue);
    }




    /**
     * @param LocalPathList 保存广告图存储路径
     */
    public void saveLocalAdvertImg(ArrayList<String> LocalPathList){
        if(LocalPathList == null){
            return;
        }
        putSPInt(ADVERT_SAVE_NUM,LocalPathList.size());
        for (int x = 0;x < LocalPathList.size() ; x ++){
            putSPString(ADVERT_SAVE + x, LocalPathList.get(x));
        }

    }
    /**
     * @return 获取广告图存储路径
     */
    public ArrayList<String> getLocalAdvert(){
        int size = getSPInt(ADVERT_SAVE_NUM, 0);
        ArrayList<String> arrayList = new ArrayList<>();
        for (int x = 0; x < size ; x ++){
            arrayList.add(getSPString(ADVERT_SAVE + x,""));
        }

        return arrayList;
    }
}
